//
//  HoursSetViewController.m
//  Lightthings
//
//  Created by 王岩 on 2024/11/29.
//

#import "HoursSetViewController.h"

@interface HoursSetViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btn12;
@property (weak, nonatomic) IBOutlet UIButton *btn24;
/* <#note#> */
@property (nonatomic, assign) DeviceCommandEnum action;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;

@end

@implementation HoursSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        
    self.action = [self.metaInfo[@"action"] intValue];
    if (self.action == Command_12_24_Set) {
        self.jy_title = self.titleLab.text = SWString(@"12/24小时设置");
        [self.btn12 setTitle:SWString(@"12小时制") forState:0];
        [self.btn12 setIconInLeftWithSpacing:10];
        [self.btn24 setTitle:SWString(@"24小时制") forState:0];
        [self.btn24 setIconInLeftWithSpacing:10];
    }else if (self.action == Command_LengthSet) {
        self.jy_title = self.titleLab.text = SWString(@"公英制设置");
        [self.btn12 setTitle:SWString(@"公制") forState:0];
        [self.btn12 setIconInLeftWithSpacing:10];
        [self.btn24 setTitle:SWString(@"英制") forState:0];
        [self.btn24 setIconInLeftWithSpacing:10];
    }else if (self.action == Command_TemperatureSet) {
        self.jy_title = self.titleLab.text = SWString(@"温度单位设置");
        [self.btn12 setTitle:SWString(@"摄氏度") forState:0];
        [self.btn12 setIconInLeftWithSpacing:10];
        [self.btn24 setTitle:SWString(@"华氏度") forState:0];
        [self.btn24 setIconInLeftWithSpacing:10];
    }
    
    NSString * uuid = self.metaInfo[@"uuid"];
    NSArray * list = [FRQBluetoothManager shareManager].curPeripherals;
    for (CBPeripheral * per in list) {
        if ([per.identifier.UUIDString isEqualToString:uuid]) {
            self.peripheral = per;
        }
    }
    
    [SWInitializer jy_initRedButtonWithTarget:self action:@selector(clickSureBtnAction) frame:CGRectMake(0, 0, kScreenWidth - 100, 50) title:SWString(@"Confirm") on:self.view layout:^(MASConstraintMaker * _Nonnull make) {
        kMakeLV(50);
        kMakeBV(-100);
        kMakeWHV(kScreenWidth-100, 50);
    }];
    
    [self readData];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)readData {
    if (self.action == Command_12_24_Set) {
        [WatchCommandTool.sharedInstance readStormCommand:Command_12_24_Set ToPeripheral:self.peripheral reBack:^(NSString * _Nonnull backData) {
            if (backData) {
                NSString * valueStr = [backData substringWithRange:NSMakeRange(26, 2)];
                unsigned long value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
                NSLog(@"获取数值：%lu",value);
                self.btn12.selected  = value == 1;
                self.btn24.selected  = value == 2;
            }
        }];
    }else if (self.action == Command_LengthSet) {
        [WatchCommandTool.sharedInstance readStormCommand:Command_LengthSet ToPeripheral:self.peripheral reBack:^(NSString * _Nonnull backData) {
            if (backData) {
                NSString * valueStr = [backData substringWithRange:NSMakeRange(26, 2)];
                unsigned long value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
                NSLog(@"获取数值：%lu",value);
                self.btn12.selected  = value == 0;
                self.btn24.selected  = value == 1;
            }
        }];
    }else if (self.action == Command_TemperatureSet) {
        [WatchCommandTool.sharedInstance readStormCommand:Command_TemperatureSet ToPeripheral:self.peripheral reBack:^(NSString * _Nonnull backData) {
            if (backData) {
                NSString * valueStr = [backData substringWithRange:NSMakeRange(26, 2)];
                unsigned long value = strtoll([[NSString stringWithFormat:@"0x%@",valueStr] UTF8String], 0, 16);
                NSLog(@"获取数值：%lu",value);
                self.btn12.selected  = value == 0;
                self.btn24.selected  = value == 1;
            }
        }];
    }
}

- (IBAction)click12Btn:(UIButton *)sender {
    self.btn12.selected = YES;
    self.btn24.selected = NO;
}
- (IBAction)click24Btn:(UIButton *)sender {
    self.btn12.selected = NO;
    self.btn24.selected = YES;
}

- (void)clickSureBtnAction {
    if (self.action == Command_12_24_Set) {
        int a = self.btn12.selected?1:2;
        unsigned char byte0 = (a & 0xff);
        [[WatchCommandTool sharedInstance] writeStormCommand:Command_12_24_Set ToPeripheral:self.peripheral Value:@[@(byte0)] reBack:^(NSString * _Nonnull backData) {
            if (backData) {
                if ([backData hasSuffix:@"00"]) {
                    toast(SWString(@"Success"));
                }else {
                    toast(SWString(@""));
                }
            }
        }];
    }else if (self.action == Command_LengthSet) {
        int a = self.btn12.selected?0:1;
        unsigned char byte0 = (a & 0xff);
        [[WatchCommandTool sharedInstance] writeStormCommand:Command_LengthSet ToPeripheral:self.peripheral Value:@[@(byte0)] reBack:^(NSString * _Nonnull backData) {
            if (backData) {
                if ([backData hasSuffix:@"00"]) {
                    toast(SWString(@"Success"));
                }else {
                    toast(SWString(@""));
                }
            }
        }];
    }else if (self.action == Command_TemperatureSet) {
        int a = self.btn12.selected?0:1;
        unsigned char byte0 = (a & 0xff);
        [[WatchCommandTool sharedInstance] writeStormCommand:Command_TemperatureSet ToPeripheral:self.peripheral Value:@[@(byte0)] reBack:^(NSString * _Nonnull backData) {
            if (backData) {
                if ([backData hasSuffix:@"00"]) {
                    toast(SWString(@"Success"));
                }else {
                    toast(SWString(@""));
                }
            }
        }];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
